<#assign ctx=request.contextPath />
<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <title>随笔添加</title>
    <script src="${ctx}/js/jquery.min.js"></script>
    <script src="${ctx}/layui/layui.js"></script>
    <link href="${ctx}/layui/css/layui.css" rel="stylesheet">
    <link rel="stylesheet" href="${ctx}/css/ztree/metro/ztree.css" media="all"/>
    <link rel="stylesheet" href="${ctx}/css/treeselect.css" media="all"/>
    <style>
        .layui-form-item {
            width: 500px;
        }

        .ztree {
            height: 260px;
            overflow-y: auto;
        }
    </style>
</head>
<body>

<div class="site-text site-block" style=" border:1px solid #ccc; padding:20px; margin:20px;">
    <form class="layui-form">
        <div class="layui-form-item">
            <label class="layui-form-label">标题</label>
            <div class="layui-input-block">
                <input type="text" name="title" lay-verify="required" class="layui-input"/>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">分类</label>
            <div class="layui-input-block">
                <select name="lang">
                    <#list tagMap['langList'] as tag>
                    <option value="${tag.id}">${tag.type}</option>
                    </#list>
                    <option value="0">其他</option>
                </select>
            </div>
        </div>
        <#--<div class="layui-form-item" id="div_project">
            <label class="layui-form-label">相关项目</label>
            <div class="layui-input-block">
                <select id="project" name="project">
                    <option value="0" selected>无</option>
                <#list tagMap['projectList'] as tag>
                    <option value="${tag.id}">${tag.name}</option>
                </#list>
                </select>
            </div>
        </div>-->
        <div class="layui-form-item" id="div_isPublic">
            <label class="layui-form-label">是否公开</label>
            <div class="layui-input-block">
                <input type="checkbox" id="isPublic" name="isPublic" lay-skin="switch" lay-text="ON|OFF" checked>
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">内容</label>
                <div class="layui-input-block">
                <#--<input type="text" name="addCode" lay-verify="required" class="layui-input"/>-->
                    <textarea class="layui-textarea" name="ps" style="width: 390px;"></textarea>
                </div>
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-input-block">
            <#--<a onclick="window.history.go(-1);" class="layui-btn layui-btn-primary">返回</a>-->
                <button class="layui-btn layui-btn-normal" lay-submit="" lay-filter="formDemo">添加</button>
            </div>
        </div>
    </form>
</div>


<script>

    layui.config({
        base: '${ctx}/js/' //自定义组件的目录--相对路径
    }).use(['layer', 'form'], function () {
        var layer = layui.layer;
        var form = layui.form;
        form.on('submit(formDemo)',
                function (data) {
                    var obj = data.field;
                    check(obj);
                    console.log(obj);
                    $.ajax({
                        url: "${ctx}/addEssay",
                        type: "post",
                        data: obj,
                        success: function (res) {
                            if (res.status == 0) {
                                layer.msg(res.msg, {icon: 6, time: 600}, function () {
                                    location.href = '${ctx}/essayList'
                                });
                            } else {
                                layer.msg(res.msg, {icon: 5})
                            }
                        }
                    });
                    return false
                });

    });

    function check(data) {
        data.isPublic = data.isPublic == "on" ? 0 : 1;
        if (data.type == "3") {
            data.url = "";
        } else {
            data.isPublic = 0;
        }
    }

</script>
</body>
</html>
